﻿using System.ComponentModel.Composition;
using Mari.SmartHotel.Core;
using Microsoft.Services.Clab.UI.Mvvmc;
using Microsoft.Services.Clab.UI.Mvvmc.Composition;

namespace Mari.SmartHotel.Shell
{
    [ControllerExport("Shell")]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ShellController : SHControllerBase
    {
        #region ViewModel reference
        public new ShellViewModel ViewModel { get { return base.ViewModel as ShellViewModel; } }
        #endregion

        #region Controller Initialization
        protected override IViewModel CreateViewModel()
        {
            return new ShellViewModel();
        }

        protected override void CacheMetadata(ControllerMetadata metadata)
        {
            base.CacheMetadata(metadata);

            metadata.MapActionToMethod<string>("LaunchFunction", LaunchFunctionExecuted);
            metadata.MapActionToMethod<string, KeyValueCollection>("LaunchFunctionWithParameters", LaunchFunctionWithParametersExecuted);
            metadata.MapActionToMethod<string, object>("LaunchFunctionWithParameter", LaunchFunctionWithParameterExecuted);
            metadata.MapActionToMethod<IController>("TabClosing", TabClosingExecuted);
        }
        #endregion

        #region LaunchFunction Action
        private void LaunchFunctionExecuted(string controllerName)
        {
            this.ChildControllers.Create(controllerName, onInitializedAction: c => this.ViewModel.FocusedChildController = c);
        }
        #endregion

        #region LaunchFunctionWithParameter Action
        public void LaunchFunctionWithParameterExecuted(string controllerName, object initParameter)
        {
            this.ChildControllers.Create(controllerName, initParameters: initParameter, onInitializedAction: c => this.ViewModel.FocusedChildController = c);
        }
        #endregion

        #region LaunchFunctionWithParameters Action
        public void LaunchFunctionWithParametersExecuted(string controllerName, KeyValueCollection initParameters)
        {
            this.ChildControllers.Create(controllerName,
                initParameters: initParameters,
                onInitializedAction: c => this.ViewModel.FocusedChildController = c);
        }
        #endregion


        #region TabClosing Action
        public void TabClosingExecuted(IController controller)
        {
            controller.Dispose();
        }
        #endregion

    }
}
